<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/2
 * Time: 16:57
 */
namespace app\admin\controller;

use app\admin\Controller;
use think\Session;
use think\Db;

class Special extends Controller
{

    /**
     * 专题列表
     */
    public function index(){

        $params = $this->request->param();
        $maps = [];
        if(isset($params['name'])&&$params['name']!=''){
            $maps['name'] = array('like','%'.trim($params['name']).'%');
        }


        // 分页查询 每页显示10条
        $cates = Db::table('kyf_special')->where($maps)->order('ssort asc')->paginate(10,false,$this->request->param());
        $res = $cates->toArray();
        $pages = $cates->render();

        $this->view->assign('list',$res['data']);
        $this->view->assign('total',$res['total']);
        $this->view->assign('page',$pages);

        return $this->view->fetch();
    }


    /**
     * 添加专题
     */
    public function add(){

        if($this->request->isPost()){
            $params = $this->request->param();

            $name = isset($params['name'])?trim($params['name']):'';
            $ssort = isset($params['ssort'])?intval($params['ssort']):'';
            $desc = isset($params['desc'])?trim($params['desc']):'';
            $contents = isset($params['contents'])?trim($params['contents']):'';
            $status = isset($params['status'])?intval($params['status']):1;

            $data = array();
            $data['ssort'] = $ssort;
            $data['name'] = $name;
            $data['desc'] = $desc;
            $data['status'] = $status;
            $data['contents'] = $contents;
            $data['create_time'] = time();


            $goodsid = Db::table('kyf_special')->insert($data,false,true);
            if($goodsid > 0){
                return ajax_return(array(),'添加成功');
            } else {
                return ajax_return(array(),'提交失败');
            }

        }

        return $this->view->fetch();
    }


    /**
     * 修改专题
     */
    public function update(){

        $params = $this->request->param();
        $id  = isset($params['id'])?intval($params['id']):0;
        if($id==0){
            exit('参数错误');
        }

        if($this->request->isPost()){

            $name = isset($params['name'])?trim($params['name']):'';
            $ssort = isset($params['ssort'])?intval($params['ssort']):'';
            $desc = isset($params['desc'])?trim($params['desc']):'';
            $contents = isset($params['contents'])?trim($params['contents']):'';
            $status = isset($params['status'])?intval($params['status']):1;

            $data = array();
            $data['ssort'] = $ssort;
            $data['name'] = $name;
            $data['desc'] = $desc;
            $data['status'] = $status;
            $data['contents'] = $contents;
            $data['create_time'] = time();

            $srt = Db::table('kyf_special')->where('sid='.$id)->update($data);

            if($srt){
                return ajax_return(array(),'修改成功');
            } else {
                return ajax_return(array(),'修改失败');
            }

        } else {
            $sinfo = Db::table('kyf_special')->where('sid='.$id)->find();
            $this->view->assign('sinfo',$sinfo);
            return $this->view->fetch();
        }

    }

    /**
     * 删除专题
     */
    public function del(){
        $params = $this->request->param();
        $id = isset($params['id'])?intval($params['id']):0;
        if($id==0){
            return ajax_return(array('code'=>1,'msg'=>'参数错误'));
        }
        $rt = Db::table('kyf_special')->where('sid='.$id)->delete();
        if($rt){
            return ajax_return(array(),'删除成功');
        } else {
            return ajax_return(array(),'删除失败',1);
        }
    }

    /**
     * 向专题内添加商品
     */
    public function addgoods(){

        $params = $this->request->param();
        $id  = isset($params['id'])?intval($params['id']):0;

        if($this->request->isPost()){

            $id = isset($params['id'])?intval($params['id']):0;

            $goods_and_price = trim($params['goods_and_price']);

            $str = str_ireplace(PHP_EOL, ',', $goods_and_price);
            $arr = explode(',', $str);
            foreach ($arr as $key => $value) {
                if(strpos($value,'|')!==false) {
                    $arr = explode('|', $value);
                    $data['goods_id'] = intval($arr[0]);
                    $data['s_price'] = fmoney(trim($arr[1]));
                    $data['sid'] = $id;
                    $data['status'] = 1;
                    $data['create_time'] = time();
                    $srt = Db::table('kyf_special_goods')->insert($data,false,true);
                }
            }
            return ajax_return(array(),'操作成功');
        } else {
            $slist = Db::table('kyf_special')->where('status=1')->select();
            $sinfo = Db::table('kyf_special')->where('sid='.$id)->find();
            $this->view->assign('sinfo',$sinfo);
            $this->view->assign('slist',$slist);
            return $this->view->fetch();
        }

    }


    /**
     * 专题商品列表
     * @return string
     */
    public function specialGoods(){

        $params = $this->request->param();
        $maps = [];
        if(isset($params['sid'])&&$params['sid']!=''){
            $maps['sid'] = array('eq',intval($params['sid']));
        }


        // 分页查询 每页显示10条
        $cates = Db::table('kyf_special_goods')->where($maps)->order('id asc')->paginate(10,false,$this->request->param());
        $res = $cates->toArray();
        $pages = $cates->render();

        foreach ($res['data'] as &$val){
            $val['s_price'] = fmoney($val['s_price'],false);
            $val['sname'] = Db::table('kyf_special')->where('sid='.$val['sid'])->value('name');
            $val['goods_name'] = Db::table('kyf_goods')->where('goods_id='.$val['goods_id'])->value('goods_name');
        }

        $this->view->assign('list',$res['data']);
        $this->view->assign('total',$res['total']);
        $this->view->assign('page',$pages);

        return $this->view->fetch();
    }

    /**
     * 删除专题商品
     * @return \think\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function delsgoods(){
        $params = $this->request->param();
        $id = isset($params['id'])?intval($params['id']):0;
        if($id==0){
            return ajax_return(array('code'=>1,'msg'=>'参数错误'));
        }
        $rt = Db::table('kyf_special_goods')->where('id='.$id)->delete();
        if($rt){
            return ajax_return(array(),'删除成功');
        } else {
            return ajax_return(array(),'删除失败',1);
        }
    }


}